package com.ny.controller;
import com.ny.entity.Classrooms;
import com.ny.entity.Users;

import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;
    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        Users user = userService.login(username, password);
        session.setAttribute("user", user);
       if (user!= null) {
            System.out.println(user);
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
            System.out.println(classroomsList);
            model.addAttribute("classroomList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }
// http:localhost:8082/
// 多条件筛选:  提交的路径:/user/searchClassrooms   2.提交的方法 get  3.请求数据：
@GetMapping("/searchClassrooms")
public String searchClassrooms(
        @RequestParam(required = false) String classroomName,
        @RequestParam(required = false) String capacity,
        @RequestParam(required = false) String status,
        @RequestParam(required = false) String building,
        @RequestParam(required = false) String description,
        Model model) {
    List<Classrooms> classroomList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
    System.out.println("获取到的数据"+classroomList);
    model.addAttribute("classroomList", classroomList);
    return "classroom";
}

}
